#ifndef GRAPH_H
#define GRAPH_H

#include <iostream>
#include <vector>
#include <stdio.h>
#include "string.h"
#include <string>
using namespace std;

class graph
{
 private:
  int total_node = 0;
  
  struct link_info
  {
    int node_id;
    double length;
    
    link_info(int _id, double _length)
    {
    	node_id = _id;
    	length = _length;
    }
  };

  struct graph_node
  {
    string node_name;
    int node_id;
    vector<link_info*> edges_table;
    vector<double> edges_matrix;
  };

    vector<graph_node*> node_list;
    
 public:
  void initial(int total_node,bool mode)
  {
    getchar();
    for(int i = 1; i <= total_node; i++)
      {
	graph_node* newnode = new graph_node();
	newnode->node_id = i;
	cout << "Name of node " << i << ":" ;
	getline(cin,newnode->node_name);
	node_list.push_back(newnode);
	if(mode)
	  for(int k = 0; k < total_node ; k++)
	    newnode->edges_matrix.push_back(0);
      }
	
  }

  void single_input(int id_1,int id_2,double length,bool mode)
  {
    if(!mode)
      {
	link_info* link_input = new link_info(id_2,length);
	node_list[id_1-1]->edges_table.push_back(link_input);
	//delete link_input;
      }
    else
     node_list[id_1-1]->edges_matrix[id_2-1] = length;     
  }
  
  void stream_input(bool mode)
  {
    cout << "Data input start.When input comes to the end, press s." << endl;
    cout << "You should input like this:1,2 or 1,2,3; 1,2,3s is for over" << endl;
    int id_1,id_2;
    while(1)
      {
	double length = 1;
	cin >> id_1;
	getchar();
	cin >> id_2;
	char judge;
	judge = getchar();
	if(judge == '\n')
	  {
	    single_input(id_1,id_2,length,mode);
	      continue;
	  }
	cin >> length;
	single_input(id_1,id_2,length,mode);
	judge = getchar();
	if(judge == 's')break;
      }
  }
  
  void input(bool mode)
  {
    cout << "Number of nodes:";
    cin >> total_node;
    initial(total_node,mode);
    stream_input(mode);
  }

  void ListVertexes()
  {
    for(int i = 0; i < node_list.size() ; i++)
      {
	cout << node_list[i]->node_id << ":" << node_list[i]->node_name << endl;
      }
  }

  void ListEdges(bool mode)
  {
    if(!mode)
      {	
	for(int i = 0; i < node_list.size() ; i++)
	  {
	    for(int k = 0; k < node_list[i]->edges_table.size() ; k++)
	      {
		cout << "(" << node_list[i]->node_id << ","
		     << node_list[i]->edges_table[k]->node_id << ","
		     << node_list[i]->edges_table[k]->length << ")" << endl;
	      }
	  }
      }
    else
      {
	for(int i = 0; i < node_list.size() ; i++)
	  {
	    for(int k = 0; k < node_list.size() ; k++)
	      {
		if(node_list[i]->edges_matrix[k] == 0)continue;
		cout << "(" << i+1 << "," << k+1 << ","
		     << node_list[i]->edges_matrix[k] << ")" << endl;
	      }
	  }
      }
  }
};
#endif
  
